<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_vpt_ci.class.php');


class TCMS_VPT_CI_DBC extends TCMS_VPT_ci implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select template, ci_name, ci_type, required, export_name from tcms_vpt_cis where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_template($rez_obj->TEMPLATE);
			$this->set_ci_type($rez_obj->CI_TYPE);
			$this->set_ci_name(stripslashes($rez_obj->CI_NAME));
			$this->set_required($rez_obj->REQUIRED);
			$this->set_export_name(stripslashes($rez_obj->EXPORT_NAME));

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_vpt_cis_seq');
		$template = $this->get_template();
		$ci_type = $this->get_ci_type();
		$required = $this->get_required() ? 1 : 0;
		$ci_name = addslashes($this->get_ci_name());
		$export_name = addslashes($this->get_export_name());


		if ($id) {
			$sql = "insert into tcms_vpt_cis (id, ".
									      "template, ".
									      "ci_name, ".
									      "ci_type, ".
									      "required, ".
									      "export_name ".
									      ") ".
								"values ".
									      "($id,".
									      "$template, ".
									      "'$ci_name', ".
									      "$ci_type, ".
									      "$required, ".
									      "'$export_name' ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_vpt_cis_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$template = $this->get_template();
		$ci_type = $this->get_ci_type();
		$required = $this->get_required() ? 1 : 0;
		$ci_name = addslashes($this->get_ci_name());
		$export_name = addslashes($this->get_export_name());

		$sql = "update tcms_vpt_cis set ".
						"template = $template, ".
						"ci_name = '$ci_name', ".
						"ci_type = $ci_type, ".
						"required = $required, ".
						"export_name = '$export_name' ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_vpt_cis where id = $id";
		$dbc->execute($sql);
	}


	public static function delete_all_for_vpage(DB_Connection $dbc, $vpage_id) {
		tangra_if_not_int_throw_e($vpage_id);

		$sql = "delete from tcms_vpt_cis where vpage = $vpage_id";
		$dbc->execute($sql);
	}


	public static function load_vpt_cis_for_template(DB_Connection $dbc, $template_id) {
		tangra_if_not_int_throw_e($template_id);
		$ret = array();

		$sql = "select ci.id, ci.ci_name, t.name, ci.required, ci.export_name, t.page_edit_path, t.page_manage_path, t.page_select_path, t.page_delete_path, t.capture_prefix from tcms_vpt_cis ci, tcms_ci_types t where template = $template_id and ci.ci_type = t.id order by required desc";
		$rez = $dbc->execute($sql);

		$c = 0;
		while ($rez_obj = $rez->fetch_object()) {
			$ret[$c]['id'] = $rez_obj->ID;
			$ret[$c]['ci_name'] = $rez_obj->CI_NAME;
			$ret[$c]['type'] = $rez_obj->NAME;
			$ret[$c]['required'] = $rez_obj->REQUIRED;
			$ret[$c]['export_name'] = $rez_obj->EXPORT_NAME;
			$ret[$c]['page_edit_path'] = $rez_obj->PAGE_EDIT_PATH;
			$ret[$c]['page_manage_path'] = $rez_obj->PAGE_MANAGE_PATH;
			$ret[$c]['page_select_path'] = $rez_obj->PAGE_SELECT_PATH;
			$ret[$c]['page_delete_path'] = $rez_obj->PAGE_DELETE_PATH;
			$ret[$c]['capture_prefix'] = $rez_obj->CAPTURE_PREFIX;
			$c++;
		}

		return $ret;
	}


	public static function is_unique_template_plus_export_name(DB_Connection $dbc, $ci_id, $template_id, $export_name) {
		tangra_if_not_int_throw_e($ci_id);
		tangra_if_not_int_throw_e($template_id);
		$export_name = addslashes($export_name);

		$sql = "select id from tcms_vpt_cis where id <> $ci_id and template = $template_id and export_name = '$export_name'";
		$rez = $dbc->execute($sql);

		return !$rez->is_eod();
	}


	public static function load_all_ids_for_type(DB_Connection $dbc, $type_id) {
		tangra_if_not_int_throw_e($type_id);

		$ret = array();

		$sql = "select id from tcms_vpt_cis where ci_type = $type_id";
		$rez = $dbc->execute($sql);
		while ($rez_obj = $rez->fetch_object()) {
			$ret[] = $rez_obj->ID;
		}

		return $ret;
	}


	public function load_by_vpage_and_hid(DB_Connection $dbc, $vpage_id, $hid) {
		tangra_if_not_int_throw_e($vpage_id);
		$hid = addslashes($hid);

		$ret = false;

		$sql = "select vptcis.id from tcms_vpt_cis vptcis, tcms_vpages vps where vps.id = $vpage_id and vptcis.template = vps.template and vptcis.export_name = '$hid'";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = $this->load_by_id($dbc, $rez_obj->ID);
		}

		return $ret;
	}
}